home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / lib / iceweasel / components / nsSidebar.js < prev    next >
Encoding:
Text File  |  2013-01-09  |  7.1 KB  |  191 lines

  1. //@line 43 "/tmp/buildd/iceweasel-10.0.12esr/browser/components/sidebar/src/nsSidebar.js"
  2.  
  3. Components.utils.import("resource://gre/modules/Services.jsm");
  4. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  5.  
  6. const DEBUG = false; /* set to false to suppress debug messages */
  7.  
  8. const SIDEBAR_CONTRACTID        = "@mozilla.org/sidebar;1";
  9. const SIDEBAR_CID               = Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}");
  10. const nsISidebar                = Components.interfaces.nsISidebar;
  11. const nsISidebarExternal        = Components.interfaces.nsISidebarExternal;
  12. const nsIClassInfo              = Components.interfaces.nsIClassInfo;
  13.  
  14. // File extension for Sherlock search plugin description files
  15. const SHERLOCK_FILE_EXT_REGEXP = /\.src$/i;
  16.  
  17. function nsSidebar()
  18. {
  19. }
  20.  
  21. nsSidebar.prototype.classID = SIDEBAR_CID;
  22.  
  23. nsSidebar.prototype.nc = "http://home.netscape.com/NC-rdf#";
  24.  
  25. function sidebarURLSecurityCheck(url)
  26. {
  27.     if (!/^(https?:|ftp:)/i.test(url)) {
  28.         Components.utils.reportError("Invalid argument passed to window.sidebar.addPanel: Unsupported panel URL." );
  29.         return false;
  30.     }
  31.     return true;
  32. }
  33.  
  34. /* decorate prototype to provide ``class'' methods and property accessors */
  35. nsSidebar.prototype.addPanel =
  36. function (aTitle, aContentURL, aCustomizeURL)
  37. {
  38.     debug("addPanel(" + aTitle + ", " + aContentURL + ", " +
  39.           aCustomizeURL + ")");
  40.  
  41.     return this.addPanelInternal(aTitle, aContentURL, aCustomizeURL, false);
  42. }
  43.  
  44. nsSidebar.prototype.addPersistentPanel =
  45. function(aTitle, aContentURL, aCustomizeURL)
  46. {
  47.     debug("addPersistentPanel(" + aTitle + ", " + aContentURL + ", " +
  48.            aCustomizeURL + ")\n");
  49.  
  50.     return this.addPanelInternal(aTitle, aContentURL, aCustomizeURL, true);
  51. }
  52.  
  53. nsSidebar.prototype.addPanelInternal =
  54. function (aTitle, aContentURL, aCustomizeURL, aPersist)
  55. {
  56.     // XXX Bug 620418: We shouldn't do this anymore. Instead, we should find the
  57.     // global object for our caller and use it.
  58.     var win = Services.wm.getMostRecentWindow("navigator:browser");
  59.     if (!sidebarURLSecurityCheck(aContentURL))
  60.       return;
  61.  
  62.     var uri = null;
  63.     try {
  64.       uri = Services.io.newURI(aContentURL, null, null);
  65.     }
  66.     catch(ex) { return; }
  67.  
  68.     win.PlacesUIUtils.showBookmarkDialog({ action: "add"
  69.                                          , type: "bookmark"
  70.                                          , hiddenRows: [ "description"
  71.                                                        , "keyword"
  72.                                                        , "location"
  73.                                                        , "loadInSidebar" ]
  74.                                          , uri: uri
  75.                                          , title: aTitle
  76.                                          , loadBookmarkInSidebar: true
  77.                                          }, win, true);
  78. }
  79.  
  80. nsSidebar.prototype.validateSearchEngine =
  81. function (engineURL, iconURL)
  82. {
  83.   try
  84.   {
  85.     // Make sure the URLs are HTTP, HTTPS, or FTP.
  86.     var isWeb = /^(https?|ftp):\/\//i;
  87.  
  88.     if (!isWeb.test(engineURL))
  89.       throw "Unsupported search engine URL";
  90.  
  91.     if (iconURL && !isWeb.test(iconURL))
  92.       throw "Unsupported search icon URL.";
  93.   }
  94.   catch(ex)
  95.   {
  96.     debug(ex);
  97.     Components.utils.reportError("Invalid argument passed to window.sidebar.addSearchEngine: " + ex);
  98.  
  99.     var searchBundle = Services.strings.createBundle("chrome://global/locale/search/search.properties");
  100.     var brandBundle = Services.strings.createBundle("chrome://branding/locale/brand.properties");
  101.     var brandName = brandBundle.GetStringFromName("brandShortName");
  102.     var title = searchBundle.GetStringFromName("error_invalid_engine_title");
  103.     var msg = searchBundle.formatStringFromName("error_invalid_engine_msg",
  104.                                                 [brandName], 1);
  105.     Services.ww.getNewPrompter(null).alert(title, msg);
  106.     return false;
  107.   }
  108.  
  109.   return true;
  110. }
  111.  
  112. // The suggestedTitle and suggestedCategory parameters are ignored, but remain
  113. // for backward compatibility.
  114. nsSidebar.prototype.addSearchEngine =
  115. function (engineURL, iconURL, suggestedTitle, suggestedCategory)
  116. {
  117.   debug("addSearchEngine(" + engineURL + ", " + iconURL + ", " +
  118.         suggestedCategory + ", " + suggestedTitle + ")");
  119.  
  120.   if (!this.validateSearchEngine(engineURL, iconURL))
  121.     return;
  122.  
  123.   // OpenSearch files will likely be far more common than Sherlock files, and
  124.   // have less consistent suffixes, so we assume that ".src" is a Sherlock
  125.   // (text) file, and anything else is OpenSearch (XML).
  126.   var dataType;
  127.   if (SHERLOCK_FILE_EXT_REGEXP.test(engineURL))
  128.     dataType = Components.interfaces.nsISearchEngine.DATA_TEXT;
  129.   else
  130.     dataType = Components.interfaces.nsISearchEngine.DATA_XML;
  131.  
  132.   Services.search.addEngine(engineURL, dataType, iconURL, true);
  133. }
  134.  
  135. // This function exists largely to implement window.external.AddSearchProvider(),
  136. // to match other browsers' APIs.  The capitalization, although nonstandard here,
  137. // is therefore important.
  138. nsSidebar.prototype.AddSearchProvider =
  139. function (aDescriptionURL)
  140. {
  141.   // Get the favicon URL for the current page, or our best guess at the current
  142.   // page since we don't have easy access to the active document.  Most search
  143.   // engines will override this with an icon specified in the OpenSearch
  144.   // description anyway.
  145.   var win = Services.wm.getMostRecentWindow("navigator:browser");
  146.   var browser = win.gBrowser;
  147.   var iconURL = "";
  148.   // Use documentURIObject in the check for shouldLoadFavIcon so that we
  149.   // do the right thing with about:-style error pages.  Bug 453442
  150.   if (browser.shouldLoadFavIcon(browser.selectedBrowser
  151.                                        .contentDocument
  152.                                        .documentURIObject))
  153.     iconURL = browser.getIcon();
  154.  
  155.   if (!this.validateSearchEngine(aDescriptionURL, iconURL))
  156.     return;
  157.  
  158.   const typeXML = Components.interfaces.nsISearchEngine.DATA_XML;
  159.   Services.search.addEngine(aDescriptionURL, typeXML, iconURL, true);
  160. }
  161.  
  162. // This function exists to implement window.external.IsSearchProviderInstalled(),
  163. // for compatibility with other browsers.  It will return an integer value
  164. // indicating whether the given engine is installed for the current user.
  165. // However, it is currently stubbed out due to security/privacy concerns
  166. // stemming from difficulties in determining what domain issued the request.
  167. // See bug 340604 and
  168. // http://msdn.microsoft.com/en-us/library/aa342526%28VS.85%29.aspx .
  169. // XXX Implement this!
  170. nsSidebar.prototype.IsSearchProviderInstalled =
  171. function (aSearchURL)
  172. {
  173.   return 0;
  174. }
  175.  
  176. nsSidebar.prototype.classInfo = XPCOMUtils.generateCI({classID: SIDEBAR_CID,
  177.                                                        contractID: SIDEBAR_CONTRACTID,
  178.                                                        classDescription: "Sidebar",
  179.                                                        interfaces: [nsISidebar, nsISidebarExternal],
  180.                                                        flags: nsIClassInfo.DOM_OBJECT});
  181.  
  182. nsSidebar.prototype.QueryInterface = XPCOMUtils.generateQI([nsISidebar, nsISidebarExternal]);
  183.  
  184. var NSGetFactory = XPCOMUtils.generateNSGetFactory([nsSidebar]);
  185.  
  186. /* static functions */
  187. if (DEBUG)
  188.     debug = function (s) { dump("-*- sidebar component: " + s + "\n"); }
  189. else
  190.     debug = function (s) {}
  191.